package cc.jorange.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cc.jorange.bean.Work;
import cc.jorange.dao.WorkDao;
import cc.jorange.service.WorkService;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Service
public class WorkServiceImpl implements WorkService {

	@Autowired
	private WorkDao workDao;

	@Override
	public List<Work> findAll(String createDate) {
		List<Work> list = Lists.newArrayList();
		if (createDate.equals("all")) {
			list = Lists.newArrayList(workDao.findAll());
		} else {
			list = Lists.newArrayList(workDao.findAll(createDate));
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> findInId(List<Long> ids) {
		List<Map<String, Object>> result = Lists.newArrayList();
		List<Work> works = workDao.findInId(ids);
		for (Work work : works) {
			Map<String, Object> e = Maps.newHashMap();
			e.put("id", work.getId());
			e.put("author", work.getAuthor());
			e.put("tags", work.getTagsList());
			e.put("title", work.getTitle());
			e.put("img", work.getImg());
			e.put("company", work.getCompany());
			result.add(e);
		}
		return result;
	}

	@Override
	public Work findById(Long id) {
		return workDao.findOne(id);
	}

	@Override
	public List<Work> findAll() {
		return Lists.newArrayList(workDao.findAll());
	}

}
